package com.highveldsky.leaverequest.client.ui;

import java.util.Date;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DateBox;
import com.highveldsky.leaverequest.client.service.LeaveRequestServiceAsync;
import com.highveldsky.leaverequest.shared.model.LeaveRequest;

public class NewLeaveRequestPanel extends FlowPanel {

	private Label employeeNameLabel = new Label("Employee name:");
	private Label approverNameLabel = new Label("Approver name:");
	private Label approverEmailLabel = new Label("Approver email:");
	private Label firstDayLabel = new Label("First day of leave:");
	private Label lastDayLabel = new Label("Last day of leave:");

	public TextBox employeeName = new TextBox();
	public TextBox approverName = new TextBox();
	public TextBox approverEmail = new TextBox();
	public DateTimeFormat dateFormat = DateTimeFormat.getFormat(PredefinedFormat.DATE_LONG);
	public DateBox firstDay = new DateBox();
	public DateBox lastDay = new DateBox();

	private Grid fieldGrid = new Grid(5,2);
	
	private Button submitRequest = new Button("Submit request");
	private Button cancelRequest = new Button("Cancel");
	
	private LeaveRequestServiceAsync lrService;
	
	public NewLeaveRequestPanel(LeaveRequestServiceAsync service) {
		this.lrService = service;
	}
	
	@Override
	protected void onLoad() {
		firstDay.setFormat(new DateBox.DefaultFormat(dateFormat));
		lastDay.setFormat(new DateBox.DefaultFormat(dateFormat));
		
		fieldGrid.setWidget(0, 0, employeeNameLabel);
		fieldGrid.setWidget(0, 1, employeeName);
		fieldGrid.setWidget(1, 0, approverNameLabel);
		fieldGrid.setWidget(1, 1, approverName);
		fieldGrid.setWidget(2, 0, approverEmailLabel);
		fieldGrid.setWidget(2, 1, approverEmail);
		fieldGrid.setWidget(3, 0, firstDayLabel);
		fieldGrid.setWidget(3, 1, firstDay);
		fieldGrid.setWidget(4, 0, lastDayLabel);
		fieldGrid.setWidget(4, 1, lastDay);
		
		submitRequest.addClickHandler(new SubmitHandler());
//		cancelRequest.addClickHandler(cancelHandler);
		
		this.add(fieldGrid);
		this.add(submitRequest);
		this.add(cancelRequest);
		
	}

	class SubmitHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			LeaveRequest lr = new LeaveRequest();
			lr.employeeName = employeeName.getText();
			lr.approverName = approverName.getText();
			lr.approverEmail = approverEmail.getText();
			lr.firstDay = firstDay.getValue();
			lr.lastDay = lastDay.getValue();
			try {
				lrService.submitLR(lr, new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						employeeName.setText("");
						approverName.setText("");
						approverEmail.setText("");
						firstDay.setValue(new Date());
						lastDay.setValue(new Date());
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
				});
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}
